import Vue from "vue";
import router from "./router";
// progress bar -- start
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// progress bar -- end
import { ACCESS_TOKEN } from "@/store/constant";
import whiteMenu from "@/router/whiteMenu";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

console.log("****加载权限****");

router.beforeEach((to, from, next) => {
  NProgress.start();
  //设置title
  if (to.name && to.name != "" && to.name != "undefined") {
    document.title = window.global_value.VUE_APP_DEFAULT_TITLE + "-" + to.name;
  } else {
    document.title = window.global_value.VUE_APP_DEFAULT_TITLE;
  }
  //检查token
  if (Vue.ls.get(ACCESS_TOKEN)) {
    /* has token */
    if (to.path == "/user/login") {
      //登录成功之后，不能访问login
      next({ path: "/" });
    } else {
      //放行
      if (to.path == "/bigScreen") {
        NProgress.done(); // finish progress bar
        //数据大屏切换
        console.log("to", to);
        const routeUrl = router.resolve({
          path: "/bigScreen/index",
          query: to.query,
        });
        window.open(routeUrl.href, "_blank");
      } else if (to.path == "/echart_application/BigScreen") {
        NProgress.done(); // finish progress bar
        //数据大屏切换
        console.log("to", to);
        const routeUrl = router.resolve({
          path: "/echart_application/BigScreen/index",
          query: to.query,
        });
        window.open(routeUrl.href, "_blank");
      } else {
        next();
      }
    }
  } else {
    /* has not token */
    if (whiteMenu.indexOf(to.path) !== -1) {
      //在白名单请求中，放行
      console.log("****没有token，在白名单，放行****");
      next();
    } else {
      next({ path: "/user/login", query: { redirect: to.fullPath } });
    }
  }
});

router.afterEach(() => {
  NProgress.done(); // finish progress bar
});
